<template>
	<view>
		<view class="uni-padding-wrap uni-common-mt">
			<form @submit="formSubmit" @reset="formReset">
				<view class="uni-form-item uni-column">
					<view class="title">分组名称</view>
					<input class="uni-input" :value="groName" name="nickname" placeholder="请输入分组名称" />
				</view>

				<view class="uni-btn-v">
					<button form-type="submit">保存</button>
				</view>
			</form>
		</view>
	</view>
</template>
<script>
	import myCloud from '@/common/js/cloud.js';
	import myConst from '@/common/js/const.js';
	import myUtil from '@/common/js/util.js';
	export default {
		data() {
			return {
				groName: '',
				diyData: [],
				comType: 0,
				id: ""
			}
		},
		onLoad(option) {
			if (option.groName != undefined) {
				this.groName = option.groName;
				this.action = myConst.ACTION.EDIT;
			} else {
				this.action = myConst.ACTION.ADD;
			}
			if (option.groName != undefined) {
				const data = {
					action: myConst.ACTION.QUERY,
					params: {
						groName: option.groName
					}
				}
				myCloud.callFunction(myConst.FUNC_NAME.B_GOODS_GROUP, data, (res, e) => {
					this.diyData = res[0].diyData;
					this.id = res[0]._id;
					this.comType = res[0].comType;
				}, true)
			}

		},
		methods: {
			formSubmit: function(e) {
				console.log('form发生了submit事件，携带数据为：' + JSON.stringify(e.detail.value))
				//定义表单规则
				var that = this;
				that.groName = e.detail.value.nickname;
				if (that.groName == '') {
					uni.showToast({
						title: '分组名称不能为空',
						icon: 'none'
					})
					return;
				}
				const data = {
					action: that.action,
					params: {
						id: that.id,
						diyData: that.$data.diyData,
						groName: that.$data.groName,
						comType: that.comType,
						updatetime: myUtil.timestamp()
					}
				}
				myCloud.callFunction(myConst.FUNC_NAME.B_GOODS_GROUP, data, (res, e) => {
					uni.showToast({
						title: '提交成功',
						success: () => {
							uni.navigateBack()
						}
					});
				}, true)

			},
			formReset: function(e) {
				console.log('清空数据')
			}
		}
	}
</script>

<style>
	page {
		background-color: #F4F5F6;
		height: 100%;
		font-size: 28upx;
		line-height: 1.8;
	}

	.uni-form-item .title {
		padding: 20rpx 0;
	}

	.uni-input {
		padding: 5px 12px;
	}
</style>
